package com.gitee.magic.framework.base.spring;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.env.ConfigurablePropertyResolver;

/**
 * @author start
 */
public class PropertyConfigurer extends PropertySourcesPlaceholderConfigurer {
	
	private static ConfigurablePropertyResolver propertyResolver;
	
	@Override
	protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess,
			final ConfigurablePropertyResolver propertyResolver) throws BeansException {
		super.processProperties(beanFactoryToProcess, propertyResolver);
		PropertyConfigurer.propertyResolver=propertyResolver;
	}

	public static Boolean getBoolean(String key) {
		String value = getString(key);
		if (value == null) {
			return null;
		}
		return Boolean.parseBoolean(value);
	}

	public static Boolean getBoolean(String key, Boolean defaultValue) {
		Boolean value = getBoolean(key);
		if (value == null) {
			return defaultValue;
		}
		return value;
	}

	public static Short getShort(String key) {
		String value = getString(key);
		if (value == null) {
			return null;
		}
		return Short.parseShort(value);
	}

	public static Short getShort(String key, Short defaultValue) {
		Short value = getShort(key);
		if (value == null) {
			return defaultValue;
		}
		return value;
	}

	public static Integer getInt(String key) {
		String value = getString(key);
		if (value == null) {
			return null;
		}
		return Integer.parseInt(value);
	}

	public static Integer getInt(String key, Integer defaultValue) {
		Integer value = getInt(key);
		if (value == null) {
			return defaultValue;
		}
		return value;
	}

	public static Long getLong(String key) {
		String value = getString(key);
		if (value == null) {
			return null;
		}
		return Long.parseLong(value);
	}

	public static Long getLong(String key, Long defaultValue) {
		Long value = getLong(key);
		if (value == null) {
			return defaultValue;
		}
		return value;
	}

	public static Float getFloat(String key) {
		String value = getString(key);
		if (value == null) {
			return null;
		}
		return Float.parseFloat(value);
	}

	public static Float getFloat(String key, Float defaultValue) {
		Float value = getFloat(key);
		if (value == null) {
			return defaultValue;
		}
		return value;
	}

	public static Double getDouble(String key) {
		String value = getString(key);
		if (value == null) {
			return null;
		}
		return Double.parseDouble(value);
	}

	public static Double getDouble(String key, Double defaultValue) {
		Double value = getDouble(key);
		if (value == null) {
			return defaultValue;
		}
		return value;
	}

	public static String getString(String key) {
		Object value = getContextProperty(key);
		if (value == null) {
			return null;
		}
		return String.valueOf(value);
	}

	public static String getString(String key, String defaultValue) {
		String value = getString(key);
		if (value == null) {
			return defaultValue;
		}
		return value;
	}

	public static Object getContextProperty(String name) {
		if(PropertyConfigurer.propertyResolver!=null) {
			Object value=PropertyConfigurer.propertyResolver.getProperty(name);
			if(value!=null) {
				return value;
			}
		}
		if(SpringContext.isInitContext()) {
			return SpringContext.getAppContext().getEnvironment().getProperty(name);
		}else {
			return null;
		}
	}
	
}